package com.taekwon.remoteservice;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import java.util.ArrayList;
import ibatis.MySqlMapper;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.taekwon.model.Noticeboard;
public class NoticeboardDao {
	private static SqlMapClient sqlMapClient;
	private static Logger logger = Logger.getLogger(NoticeboardDao.class);
	public NoticeboardDao() {
		sqlMapClient = MySqlMapper.getSqlMapClient();
	}
	public ArrayList selectTop5(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		ArrayList arraylist = new ArrayList();
		try{
			sqlMapClient.startTransaction(); 
			arraylist = (ArrayList)sqlMapClient.queryForList("NoticeboardDao.selectTop5",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return arraylist;
	}
	public ArrayList selectPagingWithKeyword(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		ArrayList arraylist = new ArrayList();
		try{
			sqlMapClient.startTransaction(); 
			arraylist = (ArrayList)sqlMapClient.queryForList("NoticeboardDao.selectPagingWithKeyword",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return arraylist;
	}
	public Noticeboard getCountWithKeyword(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		Noticeboard noticeboard_ = new Noticeboard();
		try{
			sqlMapClient.startTransaction(); 
			noticeboard_ = (Noticeboard)sqlMapClient.queryForObject("NoticeboardDao.getCountWithKeyword",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return noticeboard_;
	}
	public int deleteRecord(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int_ = (int)sqlMapClient.delete("NoticeboardDao.deleteRecord",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return int_;
	}
	public int updateRecord(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int_ = (int)sqlMapClient.update("NoticeboardDao.updateRecord",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return int_;
	}
	public int insertRecord(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 

			Noticeboard noticeboard_ = (Noticeboard) sqlMapClient.queryForObject("NoticeboardDao.findMax",new Noticeboard());
			noticeboard.setThread(noticeboard_.getCount_());
			logger.debug("noticeboardAfter:"+noticeboard);

			int_ = (int)sqlMapClient.update("NoticeboardDao.insertRecord",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return int_;
	}
	public int insertReplyRecord(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int param1 = noticeboard.getThread();
			int param2 = ((int)((param1-1)/100))*100;
			noticeboard.setThread(noticeboard.getThread()-1);
			noticeboard.setDepth(noticeboard.getDepth()+1);
			Noticeboard noticeboardHierarchy = new Noticeboard();
			
			logger.debug("param1:"+param1);
			logger.debug("param2:"+param2);
			
			noticeboardHierarchy.setParam1_(param1+"");
			noticeboardHierarchy.setParam2_(param2+"");
			int intUpdate = (int)sqlMapClient.update("NoticeboardDao.updateHierarchy",noticeboardHierarchy);
			logger.debug("intUpdate:"+intUpdate);
			int_ = (int)sqlMapClient.update("NoticeboardDao.insertRecord",noticeboard); 
			logger.debug("int_:"+int_);
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return int_;
	}		
	public Noticeboard findMax(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		Noticeboard noticeboard_ = new Noticeboard();
		try{
			sqlMapClient.startTransaction(); 
			noticeboard_ = (Noticeboard)sqlMapClient.queryForObject("NoticeboardDao.findMax",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return noticeboard_;
	}
	public int updateHierarchy(Noticeboard noticeboard) throws SQLException{
		logger.debug("start");
		logger.debug("noticeboard:"+noticeboard);
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int_ = (int)sqlMapClient.update("NoticeboardDao.updateHierarchy",noticeboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			logger.error("error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		logger.debug("end");
		return int_;
	}
	public static void main(String[] args) throws SQLException {
		NoticeboardDao noticeboarddao = new NoticeboardDao();
	}
}
